#import "CYNET.h"
@implementation CYNET
+ (__kindof NSURLSessionTask *)POST:(NSString *)URL
                         parameters:(id)parameters
                      responseCache:(CYHttpRequestSuccess)cyresponseCache
                            success:(CYHttpRequestSuccess)cysuccess
                            failure:(CYHttpRequestFailed)failure hasCache:(BOOL)cache
{
    int perr = 0;
    NSMutableDictionary *parm = [[NSMutableDictionary alloc] initWithDictionary:parameters];
    if([UserManager sharedUserManager].curUserInfo == nil && isLogin)
    {
        [[UserManager sharedUserManager] logout:^(BOOL success, NSString *des) {
            [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"登录已过期")];
        }];
    }
    if(StrValid([UserManager sharedUserManager].curUserInfo.id))
    {
        [parm setObject:[UserManager sharedUserManager].curUserInfo.id forKey:@"uid"];
        [parm setObject:[UserManager sharedUserManager].curUserInfo.token forKey:@"token"];
    }
    else
    {
        perr = 1;
        NSLog(ASLocalizedString(@"打印 没有 uid  %@"),[UserManager sharedUserManager].curUserInfo.id);
    }
    [PPNetworkHelper closeAES];
    [PPNetworkHelper openLog];
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
    return [PPNetworkHelper POST:URL parameters:parm responseCache:^(id responseCache) {
        if(cache)
        {
            if([[responseCache valueForKey:@"code"] intValue] == 1)
            {
                cyresponseCache([responseCache valueForKey:@"data"]);
            }
            else
            {
                failure([responseCache valueForKey:@"msg"]);
            }
        }
    } success:^(id responseObject) {
        NSLog(ASLocalizedString(@"请求url %@"),URL);
        if([[responseObject valueForKey:@"code"] intValue] == 1)
        {
            cysuccess([responseObject valueForKey:@"data"]);
        }
        else
        {
            failure([responseObject valueForKey:@"msg"]);
        }
    } failure:^(NSError *error) {
        if(perr == 0)
        {
            failure(ASLocalizedString(@"服务器返回了错误的消息"));
        }
        else
        {
            failure(ASLocalizedString(@"服务器返回了错误的消息"));
        }
    }];
};
+ (__kindof NSURLSessionTask *)POSTv2:(NSString *)URL
                         parameters:(id)parameters
                      responseCache:(CYHttpRequestSuccess)cyresponseCache
                            success:(CYHttpRequestSuccess)cysuccess
                            failure:(CYHttpRequestFailed)failure hasCache:(BOOL)cache
{
    NSMutableDictionary *parm = [[NSMutableDictionary alloc] initWithDictionary:parameters];
    NSLog(ASLocalizedString(@"请求的参数 %@"),parm);
    if([UserManager sharedUserManager].curUserInfo == nil && isLogin)
    {
        [[UserManager sharedUserManager] logout:^(BOOL success, NSString *des) {
            [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"登录已过期")];
        }];
    }
    if(StrValid([UserManager sharedUserManager].curUserInfo.id))
    {
        [parm setObject:[UserManager sharedUserManager].curUserInfo.id forKey:@"uid"];
        [parm setObject:[UserManager sharedUserManager].curUserInfo.token forKey:@"token"];
    }
    [PPNetworkHelper closeAES];
    [PPNetworkHelper openLog];
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
    return [PPNetworkHelper GET:URL parameters:parm responseCache:^(id responseCache) {
        if(cache)
        {
            if([[responseCache valueForKey:@"code"] intValue] == 1)
            {
                cyresponseCache(responseCache);
            }
            else
            {
                failure([responseCache valueForKey:@"msg"]);
            }
        }
    } success:^(id responseObject) {
        if([[responseObject valueForKey:@"code"] intValue] == 1)
        {
            cysuccess(responseObject);
        }
        else
        {
            NSString *strError = [responseObject valueForKey:@"msg"];
            if([strError isEqualToString:ASLocalizedString(@"登录信息失效")])
            {
                @try
                {
                    [userManager logout:^(BOOL success, NSString *des) {
                        [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请重新登录")];
                    }];
                }
                @catch (NSException *exception)
                {
                }
            }
            failure(strError);
        }
    } failure:^(NSError *error) {
        NSLog(ASLocalizedString(@"网络加载失败啦！"));
        failure(ASLocalizedString(@"服务器返回了错误的消息"));
    }];
};
+ (__kindof NSURLSessionTask *)POSTv3:(NSString *)URL
                           parameters:(id)parameters
                        responseCache:(CYHttpRequestSuccess)cyresponseCache
                              success:(CYHttpRequestSuccess)cysuccess
                              failure:(CYHttpRequestFailedV3)failure hasCache:(BOOL)cache
{
    NSMutableDictionary *parm = [[NSMutableDictionary alloc] initWithDictionary:parameters];
    NSLog(ASLocalizedString(@"请求的参数 %@"),parm);
    if([UserManager sharedUserManager].curUserInfo == nil && isLogin)
    {
        [[UserManager sharedUserManager] logout:^(BOOL success, NSString *des) {
            [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"登录已过期")];
        }];
    }
    if(StrValid([UserManager sharedUserManager].curUserInfo.id))
    {
        [parm setObject:[UserManager sharedUserManager].curUserInfo.id forKey:@"uid"];
        [parm setObject:[UserManager sharedUserManager].curUserInfo.token forKey:@"token"];
    }
    [PPNetworkHelper closeAES];
    [PPNetworkHelper openLog];
    [PPNetworkHelper setRequestSerializer:PPRequestSerializerJSON];
    [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
    return [PPNetworkHelper GET:URL parameters:parm responseCache:^(id responseCache) {
        if(cache)
        {
            if([[responseCache valueForKey:@"code"] intValue] == 1)
            {
                cyresponseCache(responseCache);
            }
            else
            {
                failure([responseCache valueForKey:@"code"],[[responseCache valueForKey:@"msg"] integerValue]);
            }
        }
    } success:^(id responseObject) {
        if([[responseObject valueForKey:@"code"] intValue] == 1)
        {
            cysuccess(responseObject);
        }
        else
        {
            NSString *strError = [responseObject valueForKey:@"msg"];
            if([strError isEqualToString:ASLocalizedString(@"登录信息失效")])
            {
                @try
                {
                    [userManager logout:^(BOOL success, NSString *des) {
                        [[HUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请重新登录")];
                    }];
                }
                @catch (NSException *exception)
                {
                }
            }
            failure(strError,[[responseObject valueForKey:@"code"] integerValue]);
        }
    } failure:^(NSError *error) {
        NSLog(ASLocalizedString(@"网络加载失败啦！"));
        failure(ASLocalizedString(@"服务器返回了错误的消息"),9001);
    }];
}
@end
